#include "homewig.h"
#include "ui_homewig.h"
#include "globalval.h"
#include "handoff.h"

homewig::homewig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::homewig)
{
    ui->setupUi(this);
    setWindowTitle("智能蘑菇房监控系统");
    setWindowIcon(QIcon(":/image/title.png"));


    //实例化界面
    setWig = new SetWig;
    stm32Wig = new Stm32Wig;
    mg = new mjpg;
    ho = new handoff;
    search = new Search;
    history = new History;


    //将界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(stm32Wig);
    ui->stackedWidget->addWidget(setWig);
    ui->stackedWidget->addWidget(mg);
    ui->stackedWidget->addWidget(ho);
    ui->stackedWidget->addWidget(search);
    ui->stackedWidget->addWidget(history);



    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress(*GlobalVal::mqtt_addr));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     stm32Wig, SLOT(mqttSlot(QByteArray)));

    // 加载背景图片
      initSence();
}


void homewig::initSence()
{
    // 加载背景图片
    m_Background = QPixmap(":/image/bg1.png");

    // 设置窗口尺寸为背景图片的尺寸
    setFixedSize(m_Background.width(), m_Background.height());
}

void homewig::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.drawPixmap(rect(), m_Background);
}
homewig::~homewig()
{
    delete ui;
}





